/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.front.web.product;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.CommonParameter;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.front.entity.cart.GenCart;
import com.thinkgem.jeesite.modules.product.entity.product.GenProduct;
import com.thinkgem.jeesite.modules.product.service.catalog.GenCatalogService;
import com.thinkgem.jeesite.modules.product.service.product.GenProductService;

/**
 * 产品显示Controller（FRONT）
 * @author Egan
 * @version 2016-12-15
 */
@Controller
@RequestMapping(value = "${frontPath}/front/product/genProduct")
public class GenProductFrontController extends BaseController {

	@Autowired
	GenProductService genProductService;
	
	@Autowired
	GenCatalogService genCatalogService;
	
	@ModelAttribute
	public GenProduct get(@RequestParam(required=false) String id) {
		GenProduct entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = genProductService.get(id);
		}
		if (entity == null){
			entity = new GenProduct();
		}
		return entity;
	}
	
	@RequestMapping(value = "all")
	public String list(GenProduct genProduct, HttpServletRequest request, HttpServletResponse response, Model model) {
		genProduct.setCatalogName("");
		Page<GenProduct> genProducts = genProductService.findListByCatalogNameNotRecommend(new Page<GenProduct>(request, response), genProduct);
		model.addAttribute("page", genProducts);
		model.addAttribute("uploadPath", CommonParameter.uploadPath);
		return "modules/front/good/commodity_search";
	}
	
	
	@RequestMapping(value = "searchPro")
	public String searchPro() {
		return "modules/front/good/commodity_search";
	}
	

	@RequestMapping(value = "detail")
	public String detail(GenProduct genProduct, HttpServletRequest request, HttpServletResponse response, Model model) {
		
		model.addAttribute("genCart",new GenCart());
		model.addAttribute("uploadPath", CommonParameter.uploadPath);
		genProduct = genProductService.get(genProduct);
		if(genProduct!=null){
            model.addAttribute("genProduct",genProduct);
            String imagesStr = genProduct.getImages();
            List<String> lst = new ArrayList<String>();
            if (!"".equals(imagesStr)) {
                String[] images = imagesStr.split(",");
                lst = Arrays.asList(images);
            }
            model.addAttribute("lst", lst);
            
            List<GenProduct> result = new ArrayList<GenProduct>();
            String associateStr = genProduct.getAssociateStr();
            if (!"".equals(associateStr)) {
                String[] associates = associateStr.split(",");
                
                for (String associate : associates) {
                    
                    if (!"".equals(associate)) {
                        GenProduct genPro = new GenProduct();
                        genPro.setId(associate);
                        genPro = genProductService.get(genPro);
                        result.add(genPro); 
                    }
                }
            }
            model.addAttribute("result", result);
        }
		
		
		return "modules/front/good/commodity_detail";
	}
	
	@RequestMapping(value = "developmentBoard")
	public String developmentBoard(GenProduct genProduct, HttpServletRequest request, HttpServletResponse response, Model model) {
		genProduct.setCatalogName(CommonParameter.nameKFB);
		Page<GenProduct> genProducts = genProductService.findListByCatalogNameNotRecommend(new Page<GenProduct>(request, response), genProduct);
		model.addAttribute("page", genProducts);
		model.addAttribute("uploadPath", CommonParameter.uploadPath);
		return "modules/front/good/commodity_developmentBoard";
	}
	
	@RequestMapping(value = "parts")
	public String parts(GenProduct genProduct, HttpServletRequest request, HttpServletResponse response, Model model) {
		genProduct.setCatalogName(CommonParameter.namePJ);
		Page<GenProduct> genProducts = genProductService.findListByCatalogNameNotRecommend(new Page<GenProduct>(request, response), genProduct);
		model.addAttribute("page", genProducts);
		model.addAttribute("uploadPath", CommonParameter.uploadPath);
		return "modules/front/good/commodity_parts";

	}
	
	@RequestMapping(value = "recomandparts")
	public String recomandparts(GenProduct genProduct, HttpServletRequest request, HttpServletResponse response, Model model) {
		
		Page<GenProduct> genProducts = genProductService.findRecomandParts(new Page<GenProduct>(request, response), genProduct);
		model.addAttribute("page", genProducts);
		model.addAttribute("uploadPath", CommonParameter.uploadPath);
		return "modules/front/good/commodity_recommend";

	}
	
	@RequestMapping(value = "getStockByProductId")
	@ResponseBody
	public Object getStockByProductId(GenProduct entity, HttpServletRequest request, HttpServletResponse response, Model model) {
		GenProduct product = genProductService.get(entity);
		return product;
	}
}